<template>
  <div>
    <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="showAddSpu">添加SPU</el-button>
    <el-table :data="spuList" border style="margin:20px 0">
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="SPU名称" prop="spuName" />
      <el-table-column label="SPU描述" prop="description" />
      <el-table-column label="操作">
        <template v-slot="{row,$index}">
          <el-button type="success" size="small" icon="el-icon-plus" title="添加sku" @click="showAddSku(row)" />
          <el-button type="warning" size="small" icon="el-icon-edit-outline" title="修改spu" @click="showUpdateSpu(row)" />
          <el-button type="info" size="small" icon="el-icon-info" title="查看sku列表" @click="lookSkuList(row)" />
          <el-button type="danger" size="small" icon="el-icon-delete" title="删除spu" @click="deleteSpu(row)" />
        </template>
      </el-table-column>
    </el-table>
    <!-- layout="prev,pager,next,jumper, -> ,size,total" -->
    <el-pagination
      :total="total"

      layout="prev,pager,next,jumper, -> ,sizes,total"
      :page-sizes="[3,5,7,9]"
      :page-size="limit"
      :current-page="page"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog :before-close="beforeClose" :visible.sync="dialogVisible" :title="`${spu.spuName}的sku列表`">
      <el-table v-loading="loading" :data="skuList">
        <el-table-column prop="skuName" label="名称" />
        <el-table-column prop="price" label="价格" />
        <el-table-column prop="weight" label="重量" />
        <el-table-column label="默认图片">
          <template v-slot="{row,$index}">
            <el-image :src="row.skuDefaultImg" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { getPageList, deleteSpu } from '@/api/product/spu'
import { findBySpuId } from '@/api/product/sku'

import { mapState } from 'vuex'

export default {
  name: 'SpuList',
  props: ['setShowState'],
  data() {
    return {
      page: 1,
      limit: 3,
      total: 0,
      spuList: [],
      dialogVisible: false,
      spu: {
        category3Id: '',
        description: '',
        spuName: '',
        spuImageList: [],
        spuSaleAttrList: []
      },
      skuList: [],
      loading: false
    }
  },
  computed: {
    ...mapState('category', ['category3Id'])
  },
  watch: {
    category3Id: {
      handler(newVal) {
        if (!newVal) {
          this.spuList = []
        } else {
          this.getPageSpuList()
        }
      },
      immediate: true
    }
  },
  methods: {
    // 获取spu列表的函数
    async getPageSpuList() {
      try {
        const res = await getPageList(this.page, this.limit, this.category3Id)
        console.log(res)
        this.spuList = res.data.records
        this.total = res.data.total
      } catch (error) {
        this.$message.error('获取spu列表失败')
      }
    },
    // 每页大小变化执行的回调
    handleSizeChange(num) {
      this.limit = num
      this.getPageSpuList()
    },
    // 当前页变化执行的回调
    handleCurrentChange(num) {
      this.page = num
      this.getPageSpuList()
    },
    // 点击添加spu
    showAddSpu() {
      // emit('setShowState', 2)
      this.setShowState(2)
    },
    // 点击修改spu
    showUpdateSpu(row) {
      // emit('setShowState', 2, row)
      this.setShowState(2, row)
    },
    // 点击添加sku
    showAddSku(row) {
      // emit('setShowState', 3, row)
      this.setShowState(3, row)
    },
    // 点击删除spu的逻辑
    async deleteSpu(row) {
      try {
        await deleteSpu(row.id)
        this.$message.success('删除spu成功')
        if (this.spuList.length === 1) {
          this.page -= 1
        }
        this.getPageSpuList()
      } catch (error) {
        this.$message.error('删除spu失败')
      }
    },
    // 对话框
    async lookSkuList(row) {
      this.dialogVisible = true
      this.spu = row
      this.loading = true
      try {
        const res = await findBySpuId(row.id)
        this.skuList = res.data
        this.loading = false
      } catch (error) {
        this.$message.error('获取sku列表失败')
        this.loading = false
      }
    },
    // 关闭之前执行的回调
    beforeClose() {
      this.skuList = []
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>

</style>
